/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package amor_boutique_management;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author LovingXu
 */
public class Database {
    public static ArrayList<String> getBranchList() {
        ArrayList<String> arrayList = new ArrayList<>();
        Statement sql;
        
        AmorConnection.connect();
        try {
            sql = AmorConnection.CONNECTION.createStatement();
            ResultSet results = sql.executeQuery("select * from branch");
            if (results != null) {
                while (results.next()) {
                    arrayList.add(results.getString(2));
                }
                results.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
        AmorConnection.disconnect();
        
        return arrayList;
    }
    
    public static void addBranch(String branch) {
        Statement sql;
        
        AmorConnection.connect();
        try {
            sql = AmorConnection.CONNECTION.createStatement();
            sql.executeUpdate("insert into branch(name) values ('" + branch + "')");
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        AmorConnection.disconnect();
    }
    
    public static ArrayList<String> getTypeList() {
        ArrayList<String> arrayList = new ArrayList<>();
        Statement sql;
        
        AmorConnection.connect();
        try {
            sql = AmorConnection.CONNECTION.createStatement();
            ResultSet results = sql.executeQuery("select * from type");
            if (results != null) {
                while (results.next()) {
                    arrayList.add(results.getString(2));
                }
                results.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
        AmorConnection.disconnect();
        
        return arrayList;
    }
    
    public static void addType(String type) {
        Statement sql;
        
        AmorConnection.connect();
        try {
            sql = AmorConnection.CONNECTION.createStatement();
            sql.executeUpdate("insert into type(name) values ('" + type + "')");
        } catch (SQLException ex) {
            Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        AmorConnection.disconnect();
    }
} 
